package com.zzy.controller;

import org.apache.kafka.clients.producer.*;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

/*生产者同步发送消息*/
public class SendMsgTest2 {
    public final static String TOPIC_NAME = "my-replication-topic";

    public static String kafka_ip = "192.168.21.131:9092";

    public static void main(String[] args) {

        //设置参数
        Properties properties = new Properties();
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,kafka_ip);
             //   "192.168.133.133:8082,192.168.133.133:9093,192.168.133.133:9094");

        //把发送的key从字符串序列化为字节数组
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        //把发送消息value从字符串序列化为字节数组
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName());

        //创建生产消息的客户端，传入参数
        KafkaProducer<String,String> kafkaProducer = new KafkaProducer<String,String>(properties);

        //创建消息
        //key：作用是决定往那个分区上发，value：具体要发送的消息内容
        ProducerRecord<String,String> producerRecord = new ProducerRecord<>
                (TOPIC_NAME,"mykeyvalue","hellokafka222");

        kafkaProducer.send(producerRecord, new Callback() {
            @Override
            public void onCompletion(RecordMetadata recordMetadata, Exception e) {
                if (e!=null){
                    System.out.println("发送消息失败");
                }
                if (recordMetadata!=null){
                    //消息发送的元数据为
                    System.out.println("异步发送消息结果:"+"topic"+recordMetadata.topic()+"partition"+recordMetadata.partition()
                            +"offset"+recordMetadata.offset());
                }
            }
        });
    }

}
